package io.qar.commons.tools.utils;

import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
/**
 * @Author: meng
 * @Description: 文件转换工具类
 * @Date: 2023/6/30 14:13
 * @Version: 1.0
 */
public class FileUtils {
 
	/**
	 * MultipartFile 转 File
	 * @param file
	 * @throws Exception
	 */
	public static File multipartFileToFile(MultipartFile file) throws Exception {
		File toFile = null;
		if(file.equals("")||file.getSize()<=0){
			file = null;
		}else {
			InputStream ins = null;
			ins = file.getInputStream();
			toFile = new File(file.getOriginalFilename());
			toFile = inputStreamToFile(ins, toFile);
			ins.close();
		}
		return toFile;
	}
 
 
	private static File inputStreamToFile(InputStream ins, File file) {
		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}